﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;

namespace Web.frontStage
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          if (null != Session["username"])
          {
              string connectionString = GetConnectionString();
              string sqlString;
              SqlConnection sqlCon = new SqlConnection(connectionString);
              sqlString = "select * from userDetail_Tab where user_id = " + Session["userId"];

              SqlCommand command2 = new SqlCommand(sqlString, sqlCon);
              SqlDataAdapter myAdapter2 = new SqlDataAdapter();
              myAdapter2.SelectCommand = command2;
              DataSet myDataSet2 = new DataSet();

              sqlCon.Open();

              myAdapter2.SelectCommand.ExecuteNonQuery();
              sqlCon.Close();

              myAdapter2.Fill(myDataSet2);
              Session.Add("leftDayNum", myDataSet2.Tables[0].Rows[0].ItemArray[17]);
          }

        }

        static private string GetConnectionString()
        {
            // To avoid storing the connection string in your code, 
            // you can retrieve it from a configuration file.
            return "Data Source=WEIWANG-PC\\SQLEXPRESS;Initial Catalog=iMovie;Integrated Security=True";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {





            string connectionString = GetConnectionString();
            string sqlString;

            SqlConnection sqlCon = new SqlConnection(connectionString);


            string username = username1.Text.Trim();
            string password = password1.Text.Trim();
            int leftDayNum = 0;

            sqlString = "select * from userDetail_Tab where userDetail_name =  " + "'" + username + "'";
            SqlCommand command1 = new SqlCommand(sqlString, sqlCon);
            SqlDataAdapter myAdapter = new SqlDataAdapter();
            myAdapter.SelectCommand = command1;
            DataSet myDataSet = new DataSet();


            sqlCon.Open();

            myAdapter.SelectCommand.ExecuteNonQuery();
            sqlCon.Close();

            myAdapter.Fill(myDataSet);
            if (myDataSet.Tables[0].Rows.Count == 0)
            {

                Response.Write("<script> alert ('用户名不存在O(∩_∩)O~')</script>");
            }
            else
            {
           
          

            sqlString += "and userDetail_pwd =" + "'" + password + "'";

            SqlCommand command2 = new SqlCommand(sqlString, sqlCon);
            SqlDataAdapter myAdapter2 = new SqlDataAdapter();
            myAdapter2.SelectCommand = command2;
            DataSet myDataSet2 = new DataSet();

            sqlCon.Open();

            myAdapter2.SelectCommand.ExecuteNonQuery();
            sqlCon.Close();

            myAdapter2.Fill(myDataSet2);
            if (myDataSet2.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script> alert ('您的密码不正确O(∩_∩)O~')</script>");
            }
            else
            {
                Session.Add("username", username);
                Session.Add("userId", myDataSet2.Tables[0].Rows[0].ItemArray[0]);
                Session.Add("leftDayNum", myDataSet2.Tables[0].Rows[0].ItemArray[17]);
                //Label2.Text = username;
                Response.Write("<script> alert ('登陆成功O(∩_∩)O~')</script>");
            }
           
        //    Response.Redirect("/frontStage/success.aspx");
        }
        }

        protected void Button_Login_Click(object sender, EventArgs e)
        {
            Response.Redirect("index.aspx");
        }

        protected void Button_Login_Off_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Redirect("index.aspx");
        }



    }
}